/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.Util;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Fellipe
 */
public class Thumbnail {

    public byte[] imagem(Arquivo arquivo) throws IOException {

        BufferedImage img = ImageIO.read(new ByteArrayInputStream(arquivo.getData()));

        int altura = img.getHeight();
        int largura = img.getWidth();
        int maximo = 150;
        float scale;

        System.out.println("Altura: " + altura + " Largura: " + largura);

        if (altura > largura) {
            float aux1 = 100 * maximo;
            float aux2 = aux1 / altura;
            scale = aux2 / 100;
        } else {
            float aux1 = 100 * maximo;
            float aux2 = aux1 / largura;
            scale = aux2 / 100;
        }

        int novaAltura = (int) (scale * altura);
        int novaLargura = (int) (scale * largura);

        System.out.println("Nova Altura: " + novaAltura + " Nova Largura: " + novaLargura + " Escala: " + scale);

        BufferedImage bi = new BufferedImage(novaLargura, novaAltura, BufferedImage.TYPE_INT_RGB);
        Graphics2D grph = (Graphics2D) bi.getGraphics();
        grph.scale(scale, scale);
        grph.drawImage(img, 0, 0, null);
        grph.dispose();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, arquivo.getExtension(), baos);
        baos.flush();
        return baos.toByteArray();

    }

}
